package org.example.面向过程;

import com.mysql.cj.jdbc.Driver;

import java.sql.*;

//查询
public class DemoD {
    public static void main(String[] args) {
        try {
            Class.forName(Driver.class.getName());
        }catch (ClassNotFoundException e){
            System.out.println("没有找到驱动！");
        }
        try {
            String url = "jdbc:mysql://localhost:3306/bd2005?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
            String username = "root";
            String password = "a123456";
            Connection conn = DriverManager.getConnection(url,username,password);
            String sql = "select*from emp";
            PreparedStatement ps = conn.prepareStatement(sql);
            //获取查询结果集，rs就是查询出来的table
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                //现在的rs就是第一行
                int id = rs.getInt(1);
                String name = rs.getString(2);
                String job = rs.getString("job");
                int mgr = rs.getInt(4);
                String hirDate = rs.getString(5);
                double sal = rs.getDouble(6);
                double comm = rs.getDouble("comm");
                int dept = rs.getInt("deptno");
                int state = rs.getInt(9);
                System.out.print(id+"\t");
                System.out.print(name+"\t");
                System.out.print(job+"\t");
                System.out.print(mgr+"\t");
                System.out.print(hirDate+"\t");
                System.out.print(sal+"\t");
                System.out.print(comm+"\t");
                System.out.print(dept+"\t");
                System.out.print(state+"\n");
            }
            rs.close();
            ps.close();
            conn.close();
        }catch (SQLException e){
            System.out.println(e.getMessage());
        }
    }
}
